package com.example.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.common.R;
import com.example.demo.entity.dto.HospitalAreaAddDto;
import com.example.demo.entity.dto.HospitalAreaQueryDto;
import com.example.demo.entity.pojo.HospitalArea;
import com.example.demo.service.HospitalAreaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.websocket.server.PathParam;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liye
 * @date 2025/10/9 14:30
 * @description:
 */
@RestController
@RequestMapping("/area")
@Tag(name = "院区管理")
public class HospitalAreaController {
    @Resource
    private HospitalAreaService hospitalAreaService;
    //日志对象
    Logger logger= LoggerFactory.getLogger(HospitalAreaController.class);

    //添加
    //@RequestMapping(value = "/add",method = RequestMethod.POST)//  post  get  delete  put  head  options  patch
    @PostMapping("/add")  //http://ip:8181/area/add
    @Operation(summary = "添加院区") // 启动数据验证 Validated
    public R add(@RequestBody @Validated HospitalAreaAddDto hospitalAreaAddDto){
        HospitalArea hospitalArea=new HospitalArea();
        //源对象        目的对象   把源对象里面的属性值 复制给 目的对象
        BeanUtils.copyProperties(hospitalAreaAddDto,hospitalArea);
        boolean  t= hospitalAreaService.save(hospitalArea);
        logger.info("添加院区结果：{}",t);
        return t?R.success():R.error();
    }
    //修改
   // @RequestMapping(value = "/update",method = RequestMethod.PUT)
    @PutMapping("/update")
    @Operation(summary = "修改院区")
    public R update(@RequestBody  HospitalArea hospitalArea){
        boolean  t= hospitalAreaService.updateById(hospitalArea);
        return t?R.success():R.error();
    }
    //删除
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除院区")
    public R delete(@PathVariable("id") Integer areaId){
        boolean  t= hospitalAreaService.removeById(areaId);
        return t?R.success():R.error();
    }
    //查询全部
    @GetMapping("/list")
    @Operation(summary = "查询全部院区")
    public R list(){
        return R.success(hospitalAreaService.list());
    }

    @GetMapping("/page")
    @Operation(summary = "根据名字分页查询数据")
    public R page( HospitalAreaQueryDto queryDto){
        IPage<HospitalArea>  page=hospitalAreaService.page(queryDto);
        return R.success(page.getRecords(),page.getTotal());
    }




}
